package com.sks.web.action.house;

import java.io.Serializable;
import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.stereotype.Controller;

import com.sks.bean.PageView;
import com.sks.bean.QueryResult;
import com.sks.bean.pojo.Comment;
import com.sks.bean.pojo.Revert;
import com.sks.bean.privilege.Employee;
import com.sks.service.house.CommentService;
import com.sks.service.house.RevertService;
import com.sks.service.privilege.UsersService;
import com.sks.utils.WebUtil;
import com.sks.web.action.BasicAction;
import com.sks.web.action.privilege.Permission;
import com.sks.web.formbean.house.HouseForm;

@Controller("/cms/house/comment")
public class CommentAction extends BasicAction {
	@Resource(name = "commentServiceBean") private CommentService service;
	@Resource(name = "revertServiceBean") private RevertService revertService;
	@Resource UsersService usersService;
	
	@Permission(module = "employee", privilege = "viewComment")
	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		HouseForm formbean = (HouseForm) form;
		
		PageView<Comment> pageView = new PageView<Comment>(12, formbean.getPage());
		QueryResult qr = service.getScrollData(pageView.getFirstResult(), pageView
				.getMaxresult(), " o.house.houseId = ?", new Object[]{formbean.getHouseId()});
		pageView.setQueryResult(qr);
		request.setAttribute(PAGEVIEW, pageView);
		
		return mapping.findForward(LIST);
	}
	
	@Permission(module = "employee", privilege = "updateComment")
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		HouseForm formbean = (HouseForm) form;
		service.delete((Serializable[])formbean.getCommentIds());
		return list(mapping, formbean, request, response);
	}
	
	@Permission(module = "employee", privilege = "revertComment")
	public ActionForward revert(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		HouseForm formbean = (HouseForm) form;
		Employee emp = WebUtil.getEmployee(request);
		Comment com = service.find(formbean.getCommentId());
		Revert reveret = new Revert(emp, com, formbean.getContent(), new Date());
		revertService.save(reveret);
		return listRevert(mapping, form, request, response);
	}
	
	@Permission(module = "employee", privilege = "revertComment")
	public ActionForward listRevert(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		HouseForm formbean = (HouseForm) form;
		formbean.setContent(null);
		PageView<Revert> pageView = new PageView<Revert>(12, formbean.getPage());
		QueryResult qr = revertService.getScrollData(pageView.getFirstResult(), pageView
				.getMaxresult(), " o.comment.commentId = ?", new Object[]{formbean.getCommentId()});
		
		pageView.setQueryResult(qr);
		request.setAttribute(PAGEVIEW, pageView);
		return mapping.findForward("revert");
	}
	
	@Permission(module = "employee", privilege = "revertComment")
	public ActionForward deleteRevert(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		HouseForm formbean = (HouseForm) form;
		revertService.delete((Serializable[])formbean.getRevertIds());
		return listRevert(mapping, form, request, response);
	}
}
